//引入接口
import {reqCoodsInfo,reqAddOrUpdateShopCart} from '@/api'
//引入封装身份的模块
import {getUUID} from '@/utils/uuid_token'
const state={
    goodInfo:{},
    uuid_token:getUUID()
}
const mutations={
    GETGOODINFO(state,goodInfo){
        state.goodInfo=goodInfo
    }
}
const actions={
    //获取产品信息
    async getGoodInfo({commit},skuId){
        let result = await reqCoodsInfo(skuId)
        if(result.code==200){
        commit("GETGOODINFO",result.data)
        }
    },
    //加入购物车||修改产品个数
    async addOrUpdateShopCart({commit},{skuId,skuNum}){
        let result = await reqAddOrUpdateShopCart(skuId,skuNum)
        if(result.code == 200){
            return 'ok'
        }else{
            return new Promise.reject(new Error('faile'))
        }
    },
}
const getters={
    categoryView(state){
        //state.goodInfo初始值是空对象，返回空数值（也就是undefined）就会报错
        return state.goodInfo.categoryView || {}
    },
    skuInfo(state){
        return state.goodInfo.skuInfo || {}
    },
    spuSaleAttrList(state){
        return state.goodInfo.spuSaleAttrList
    },

}
export default {
    state,
    mutations,
    actions,
    getters
}